//
//  SiCustomFoodVC.swift
//  SiliUtility
//
//  Created by Li Dong on 2023/10/19.
//

import UIKit
import collection_view_layouts

class SiCustomFoodVC: SiBaseVC {
    
    @IBOutlet weak var nameTF: UITextField!
    
    @IBOutlet weak var bottomView: UIView!
    @IBOutlet weak var allView: UIView!
    
    var collectionView: UICollectionView!
    let CellID = NSStringFromClass(SiFoodCell.self)
    var dataList = SiFoodModel.foodList
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        title = "自定义候选菜单"
        let layout = TagsLayout()
        layout.delegate = self
        layout.cellsPadding = ItemsPadding(horizontal: 10, vertical: 10)
        layout.contentPadding = ItemsPadding(horizontal: 16, vertical: 20)
        collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UINib(nibName: "SiFoodCell", bundle: nil), forCellWithReuseIdentifier: CellID)
        allView.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        bottomView.jw_corner(byRoundingCorners: [.topLeft, .topRight], radii: 20)
    }


    @IBAction func addHandler(_ sender: Any) {
        guard let str = nameTF.text else { return  }
        if str.isEmpty {
            return
        }
        if dataList.contains(where: {$0 == str}) {
            showHUDTextOnly("重复添加")
            return
        }
        nameTF.text = ""
        view.endEditing(true)
        showHUDTextOnly("添加成功")
        dataList.append(str)
        SiFoodModel.foodList = dataList
        collectionView.reloadData()
    }
    

}

extension SiCustomFoodVC: UICollectionViewDelegate, UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        dataList.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellID, for: indexPath) as! SiFoodCell
        cell.namLabel.text = dataList[indexPath.row]
        cell.deleteCallBack = {[weak self] foodName in
            guard let self = self else { return  }
            self.dataList.removeAll(where: {$0 == foodName})
            SiFoodModel.foodList = self.dataList
            collectionView.reloadData()
        }
        return cell
    }
}

extension SiCustomFoodVC: LayoutDelegate {
    func cellSize(indexPath: IndexPath) -> CGSize {
        let str = dataList[indexPath.row]
        let itemW = str.getWidth(font: UIFont.systemFont(ofSize: 14), height: 20, maxWidth: CGFloat.screenWidth-40) + 58
        return CGSize(width: itemW, height: 36)
    }
    
    
}
